<?php

@require_once(PATH_CLASS.'css.class.php');
@require_once(PATH_CLASS.'theme.class.php');


/**
 * Description of css
 *
 * @author jpsiffert
 */
class js {
  public static $js     = array();
  public static $widgets = array();
  public static $default = array(
    'path' => array(
       'root'         => './js/'
      ,'theme'        => './js/themes/%name%/'
      ,'widget'       => './js/widgets/'
      ,'widgetTheme'  => './js/themes/%name%/widgets/'
      ,'page'         => './js/pages/'
      ,'pageTheme'    => './js/themes/%name%/pages/'
      )
    ,'more' => array(
      'type' => 'text/javascript'
    )
  );

  public static $_themes  = array();
  public static $_theme;
  
  protected $src = '';
  protected $more = '';
  protected $rank = 0;
  protected $type = '';
  
  public function __construct(&$tab,$href,$rank=0,$more=array()) {
    $this->src = $href;
    
    if(!is_array($more)){
      $more = array($more);
    }
    
    if(count($more)==0){
      $more = self::$default['more'];
    }
    
    $this->more = $more;
    $this->rank = $rank;
    
    if($this->rank==0){
      $this->rank = 100+count($tab);
    }
  }
  
  public function __toString(){
    $more=array();
    foreach($this->more as $key=>$value){
      $more[] = $key.'="'.$value.'"';
    }
    
    $more = join(' ',$more);
    
    $src = self::_themifyPath($this->src);
    
    $src = str_replace('%name%', self::getThemeName(), $src);
    
    $ret = sprintf('<script src="%s" %s></script>'.chr(13).chr(10),$src,$more);
    return $ret;
  }
  
  protected static function _themifyPath($path){
    $path = str_replace('%name%', self::getThemeName(), $path);
    return $path;
  }

  protected static function _add(&$tab,$filename,$rank=0,$more=array(),$path='$root$'){
    $add=false;
    if(!is_array($filename)){
      $filename = array($filename);
    }
    
    foreach(self::$default['path'] as $key=>$val){
      $path = str_ireplace('$'.$key.'$', $val, $path);
    }
    
    foreach($filename as $key=>$val){
      if(substr($path, -1)!='/' && substr($path, -1)!='\\'){
        $path = $path.'/';
      }
      $filepath = $path.$val;
      $file = self::_themifyPath($filepath);
      if(file_exists($file) && !isset($tab[$filepath])){
        $class = __CLASS__;
        $tab[$filepath]=new $class($tab,$filepath,$rank,$more);
        $add=true;
      } else {
        $file;
      }
    }
    
    return $add;
  }

  public function getType(){
    return $this->type;
  }
  public static function add($filename,$rank=0,$more=array(),$path='$root$'){
    self::_add(self::$js, $filename, $rank, $more, $path);
  }
  
  public static function addTpl($filename,$rank=0,$more=array(),$path='$page$'){
    self::add($filename, $rank, $more, $path);
  }
  
  public static function addTheme($themeName,$themePath='$theme$'){
    
    foreach(self::$js as $key=>$js){
     if($js->getType()==theme::TYPE_THEME){
        unset(self::$js[$key]);//=null;
      }
    }
    
    self::add($themeName, 1, array(), $themePath,theme::TYPE_THEME);
    
  }
  
  public static function getThemeName(){
    return theme::getThemeName();
  }

  public static function sorter($a,$b){
    $rankA  = $a->rank;
    $rankB  = $b->rank;
    $diff   = ($rankA - $rankB);
    return ($diff);
  }  
  
  public static function getThemePath(){
    $theme = self::getThemeName();
    $ret = str_replace('$theme$', $theme, self::$default['path']['theme']);
    return $ret;
  }
  
  public static function playTheme($theme){
    self::$_theme = $theme;
    if(in_array($theme, self::$_themes)){
      self::add('theme.js',1,array(),self::getThemePath());
    }
  }
  
  protected static function _render(&$tab){
    $ret=array();
    
    if(!is_array($tab)){
      echo '';
      return;
    }
    
    $jsTb = $tab;
    
    usort($jsTb,array("js", "sorter"));
    
    foreach($jsTb as $key=>$value){
      /**
       * @var css
       */
      $value;
      $ret[]=$value->__toString();
    }

    $crLf = chr(13);
    $ret  = join($crLf,$ret);
    $tab  = array();
    
    echo $ret;
  }
  
  public static function render(){
    // On ajoute les JS de la page
    //pageContent::addPageJs();
    self::_render(self::$js);
  }

  public static function renderWidgets(){
    self::_render(self::$widgets);
  }
  
  public static function addWidget($filename,$addBoth=true){
    if(!self::_add(self::$widgets, $filename, 0, array(), '$widgetTheme$',theme::TYPE_WIDGET) || $addBoth){
      self::_add(self::$widgets, $filename, 1, array(), '$widget$',theme::TYPE_WIDGET);
    }
  }
  
}

